package com.bbx.taxi.client.widget;
import com.bbx.taxi.client.R;

import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Bitmap;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;  
import android.graphics.Paint.Style;  
import android.graphics.RectF;  
import android.graphics.drawable.BitmapDrawable;  
import android.graphics.drawable.Drawable;  
import android.util.AttributeSet;  
import android.view.View;  
import android.widget.ImageView;  
import android.widget.SeekBar;  
/** 
 * @Path com.bbx.taxi.client.widget.CircleProgressView.java
 * @Author 严旭文
 * @Date 2016-6-7 下午2:40:23
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class CircleProgressView  extends View {  
      
        Paint mPaint;  
        int progress = 30;  
        int start_degree = 90;  
        String drawtext="";
      
        public CircleProgressView(Context context, AttributeSet attrs) {  
            super(context, attrs);  
            InitResources(context, attrs);  
      
        }  
      
        int background_int;  
        int progress_int;  
        float layout_width_float;  
        int textColor_int;  
        float textSize_float;  
        int max_int;  
        Drawable thumb_double;  
        int thumb_color;
        int thumbSize_int;  
      
        private void InitResources(Context context, AttributeSet attrs) {  
            TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgress);  
      
            background_int = mTypedArray.getColor(R.styleable.CircleProgress_progressbackground, 0xFF87cfe8);  
            progress_int = mTypedArray.getColor(R.styleable.CircleProgress_progressDrawable, 0xFFabd800);  
            layout_width_float = mTypedArray.getDimension(R.styleable.CircleProgress_progresswidth, 7);  
            textColor_int = mTypedArray.getColor(R.styleable.CircleProgress_progressTextColor, 0xFFada1de);  
            textSize_float = mTypedArray.getDimension(R.styleable.CircleProgress_progressTextSize, 50);  
            max_int = mTypedArray.getInt(R.styleable.CircleProgress_progressMax, 100);  
            progress = mTypedArray.getInt(R.styleable.CircleProgress_progress, max_int/2);  
            thumb_double = mTypedArray.getDrawable(R.styleable.CircleProgress_progressthumb);  
            thumb_color=mTypedArray.getColor(R.styleable.CircleProgress_progressthumbColor, 0xFFada1de);  
            thumbSize_int = mTypedArray.getInt(R.styleable.CircleProgress_progressthumbSize, 30);  
            mTypedArray.recycle();  
      
            if (thumb_double == null) {  
                Bitmap bitmap = Bitmap.createBitmap(thumbSize_int, thumbSize_int, Bitmap.Config.ARGB_8888);  
                // 图片画片  
                Canvas cas = new Canvas(bitmap);  
                Paint paint = new Paint();  
                paint.setStyle(Style.FILL);  
                paint.setColor(thumb_color);  
                int center = thumbSize_int / 2;  
                int radius = center - 4;  
                cas.drawCircle(center, center, radius, paint);  
                thumb_double = new BitmapDrawable(getResources(), bitmap);  
            }  
      
            mPaint = new Paint();  
            mPaint.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/num.ttf"));
        }  
      
        @Override  
        protected void onDraw(Canvas canvas) {  
            super.onDraw(canvas);  
            drawProgressView(canvas);  
        }  
      
        private void drawProgressView(Canvas canvas) {  
            InitOval(canvas);  
            drawBackground(canvas);  
            drawProgress(canvas);  
            drawProgressText(canvas);  
        }  
      
        RectF oval;  
      
        private void InitOval(Canvas canvas) {  
            int center = getWidth() / 2;  
            int radius = (int) (center  - thumbSize_int / 2);  
            // 画布中央减去半径就是左上角  
            int left_top = center - radius;  
            // 画布中央加上半径就是右下角  
            int right_bottom = center + radius;  
      
            if (oval == null) {  
                oval = new RectF(left_top, left_top, right_bottom, right_bottom);  
            }  
        }  
      
//        /** 
//         * 绘制进度文字 
//         *  
//         * @param canvas 
//         */  
//        private void drawProgressText(Canvas canvas) {  
//      
//            mPaint.setTextSize(textSize_float);  
//            mPaint.setColor(textColor_int);  
//            mPaint.setStrokeWidth(2);  
//            mPaint.setStyle(Style.FILL);  
//            String progresstext = progress + "%";  
//            float text_left = (getWidth() - mPaint.measureText(progresstext)) / 2;  
//            FontMetrics fontMetrics = mPaint.getFontMetrics();  
//            // 绘制文字是底部对齐  
//            float text_top = (float) ((getHeight() / 2 + Math.ceil(fontMetrics.descent - fontMetrics.ascent) / 2));  
//            canvas.drawText(progresstext, text_left, text_top, mPaint);  
//        }  
        /** 
         * 绘制进度文字 ,绘画秒
         *  
         * @param canvas 
         */  
        private void drawProgressText(Canvas canvas) {  
      
            mPaint.setTextSize(textSize_float);  
            mPaint.setColor(textColor_int);  
            mPaint.setStrokeWidth(2);  
            mPaint.setStyle(Style.FILL);    
            float text_left = (getWidth() - mPaint.measureText(drawtext)) / 2;  
            FontMetrics fontMetrics = mPaint.getFontMetrics();  
            // 绘制文字是底部对齐  
            float text_top = (float) ((getHeight() / 2 + Math.ceil(fontMetrics.descent - fontMetrics.ascent) / 3));  
            canvas.drawText(drawtext, text_left, text_top, mPaint);  
        }  
        private void drawProgress(Canvas canvas) {  
            // 设置进度  
            mPaint.setColor(progress_int);  
            mPaint.setStrokeWidth(layout_width_float);  
            float seek = 0;  
            if (max_int > 0) {  
                seek = (float) progress / max_int * 360;  
            }  
            canvas.drawArc(oval, start_degree, seek, false, mPaint);  
      
            canvas.save();  
            int center = getWidth() / 2;  
            int radius = (int) (center - thumbSize_int / 2);  
      
            double cycle_round = (seek + start_degree) * Math.PI / 180;  
            float x = (float) (Math.cos(cycle_round) * (radius) + center - thumbSize_int / 2);  
            float y = (float) (Math.sin(cycle_round) * (radius) + center - thumbSize_int / 2);  
            
            if(thumb_double!=null){
            thumb_double.setBounds(0, 0, thumbSize_int, thumbSize_int);  
            canvas.translate(x, y);  
            thumb_double.draw(canvas);
            }
            canvas.restore();  
        }  
      
        private void drawBackground(Canvas canvas) {  
      
            mPaint.setStrokeWidth(layout_width_float);  
            mPaint.setStyle(Paint.Style.STROKE);  
            mPaint.setAntiAlias(true);  
            // 设置背景  
            mPaint.setColor(background_int);  
            canvas.drawArc(oval, start_degree, 360, false, mPaint);  
      
        }  
      
        /** 
         * 查看Seekbar源码 
         *  
         * @param progress 
         */  
        public synchronized void setProgress(int progress) {  
            if (progress > max_int) {  
                progress = max_int;  
            }  
            this.progress = progress;  
            postInvalidate();  
        }  
        public synchronized void setProgress(int progress,String drawtext) {  
            if (progress > max_int) {  
                progress = max_int;  
            }  
            this.progress = progress;  
            this.drawtext=drawtext;
            postInvalidate();  
        }  
      
        public int getProgress() {  
            return this.progress;  
        }  
      
        @Override  
        protected void onDetachedFromWindow() {  
            super.onDetachedFromWindow();  
            if (thumb_double != null) {  
                thumb_double.setCallback(null);  
                thumb_double = null;  
            }  
        }  
      
    }  